//
//  SharedViewController.m
//  QuickCure
//
//  Created by haoge on 16/9/10.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "SharedViewController.h"
#import <SVProgressHUD.h>
@interface SharedViewController ()

@property (nonatomic,weak) UIImageView *sharedImg;

@property (nonatomic,weak) UIImageView *lineImgView;

@property (nonatomic,weak) UIImageView *imageView;

@end

@implementation SharedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置Logo
    [self creatLogo];
    //设置分享方式
//    [self creatShared];
    
    [self shareSecetionCreate];
    
}

-(void)creatLogo {
 
    self.view.backgroundColor = [UIColor colorWithRed:0.974 green:1.000 blue:0.961 alpha:1.000];
    
    //实例化Logo的图片
    UIImageView * imageView =  [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"product_img_logo_nor.png"]];
    [self.view addSubview:imageView];
    
    //约束
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(56);
        make.centerX.equalTo(self.view.mas_centerX);
        make.size.equalTo(CGSizeMake(67, 88));
    }];
    
    //实例化下面logo的图片
    UIImageView * imageView2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"product_text_logo_nor.png"]];
    self.imageView = imageView2;
    [self.view addSubview:imageView2];
    
    //Logo2的约束
    [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView.mas_bottom).offset(10);
        make.centerX.equalTo(self.view.mas_centerX);
        make.size.equalTo(CGSizeMake(83, 49));
    }];
}

-(void)shareSecetionCreate {
    
    UILabel *friendLabel = [[UILabel alloc]init];
    friendLabel.text = @"分享给好友";
    friendLabel.font = kFONT14;
    friendLabel.textColor = kThemeTextColor;
    [self.view addSubview:friendLabel];
    
    [friendLabel makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-80);
        make.centerX.equalTo(self.view);
    }];
    
    UIImageView *lineImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fengexian"]];
    [self.view addSubview:lineImgView];
    self.lineImgView = lineImgView;
    
    [lineImgView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(friendLabel.top).offset(-3);
        make.size.equalTo(CGSizeMake(280, 2));
    }];
    
    UIButton *qqBtn = [self setupSharedBtnWithImage:[UIImage imageNamed:@"qqIcon"] sharedTag:1];
    [self.view addSubview:qqBtn];
    
    [qqBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lineImgView.left).offset(20);
        make.bottom.equalTo(self.lineImgView.top).offset(-6);
        make.size.equalTo(CGSizeMake(50, 50));
    }];
    
    UIButton *wxBtn = [self setupSharedBtnWithImage:[UIImage imageNamed:@"wxIcon"] sharedTag:2];
    [self.view addSubview:wxBtn];
    
    [wxBtn makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.lineImgView.mas_centerX);
        make.bottom.equalTo(qqBtn.bottom);
        make.size.equalTo(CGSizeMake(50, 50));
    }];
    
    UIButton *wbBtn = [self setupSharedBtnWithImage:[UIImage imageNamed:@"weiboIcon"] sharedTag:3];
    [self.view addSubview:wbBtn];
    
    [wbBtn makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lineImgView.right).offset(-20);
        make.bottom.equalTo(qqBtn.bottom);
        make.size.equalTo(CGSizeMake(50, 50));
    }];
    
}

-(UIButton *)setupSharedBtnWithImage:(UIImage*)image sharedTag:(NSInteger)sharedTag {
    
    UIButton *sharedBtn = [[UIButton alloc]init];
    [sharedBtn setBackgroundImage:image forState:UIControlStateNormal];
    sharedBtn.tag = sharedTag;
    sharedBtn.layer.cornerRadius = 5;
    sharedBtn.clipsToBounds = YES;
    [sharedBtn addTarget:self action:@selector(sharedBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    return sharedBtn;
    
}

-(void)sharedBtnClick:(UIButton *)sender {
    [self btnClickedWithTag:sender.tag];
}

-(void) btnClickedWithTag:(NSInteger)tag {
    
    NSURL *url;
    
    switch (tag) {
        case 1:
            //qq
            url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/qq/id444934666?mt=8"];
            break;
            
        case 2:
            url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/wei-xin/id414478124?mt=8"];
            break;
            
        case 3:
            //weibo
            url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/id350962117?mt=8"];
            break;
    }
    
    [self clickToShareWithText:@"正在跳转到app store" url:url];
    
}

-(void)clickToShareWithText:(NSString *)text url:(NSURL *)url {
    
    [SVProgressHUD showSuccessWithStatus:text];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        [[UIApplication sharedApplication] openURL:url];
    });
}

@end
